

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Advanced topics &mdash; rosjava_core 0.0.0-SNAPSHOT documentation</title>
    
    <link rel="stylesheet" href="_static/haiku.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="_static/print.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '0.0.0-SNAPSHOT',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <script type="text/javascript" src="_static/theme_extras.js"></script>
    <link rel="top" title="rosjava_core 0.0.0-SNAPSHOT documentation" href="index.html" />
    <link rel="prev" title="Getting started" href="getting_started.html" /> 
  </head>
  <body>
      <div class="header"><h1 class="heading"><a href="index.html">
          <span>rosjava_core 0.0.0-SNAPSHOT documentation</span></a></h1>
        <h2 class="heading"><span>Advanced topics</span></h2>
      </div>
      <div class="topnav">
      
        <p>
        «&#160;&#160;<a href="getting_started.html">Getting started</a>
        &#160;&#160;::&#160;&#160;
        <a class="uplink" href="index.html">Contents</a>
        </p>

      </div>
      <div class="content">
        
        
  <div class="section" id="advanced-topics">
<h1>Advanced topics<a class="headerlink" href="#advanced-topics" title="Permalink to this headline">¶</a></h1>
<div class="section" id="listeners">
<h2>Listeners<a class="headerlink" href="#listeners" title="Permalink to this headline">¶</a></h2>
<p>Because rosjava provides a primarily asynchronous API, many classes which allow
you to provide event listeners. For example,
<a class="reference external" href="javadoc/org/ros/node/topic/PublisherListener.html#">PublisherListener</a>s allow you to react to
lifecycle events of a <a class="reference external" href="javadoc/org/ros/node/topic/Publisher.html#">Publisher</a>. The snippet
below adds a <a class="reference external" href="javadoc/org/ros/node/topic/PublisherListener.html#">PublisherListener</a> that will log a
warning message if the <a class="reference external" href="javadoc/org/ros/node/topic/Publisher.html#">Publisher</a> fails to
register with the master.</p>
<div class="highlight-java"><div class="highlight"><pre><span class="n">Node</span> <span class="n">node</span><span class="o">;</span>
<span class="n">Publisher</span><span class="o">&lt;</span><span class="n">std_msgs</span><span class="o">.</span><span class="na">String</span><span class="o">&gt;</span> <span class="n">publisher</span><span class="o">;</span>

<span class="o">...</span>

<span class="n">publisher</span><span class="o">.</span><span class="na">addListener</span><span class="o">(</span><span class="k">new</span> <span class="n">DefaultPublisherListener</span><span class="o">()</span> <span class="o">{</span>
  <span class="nd">@Override</span>
  <span class="kd">public</span> <span class="kt">void</span> <span class="nf">onMasterRegistrationFailure</span><span class="o">(</span><span class="n">Publisher</span><span class="o">&lt;</span><span class="n">std_msgs</span><span class="o">.</span><span class="na">String</span><span class="o">&gt;</span> <span class="n">registrant</span><span class="o">)</span> <span class="o">{</span>
    <span class="n">node</span><span class="o">.</span><span class="na">getLog</span><span class="o">().</span><span class="na">warn</span><span class="o">(</span><span class="s">&quot;Publisher failed to register: &quot;</span> <span class="o">+</span> <span class="n">registrant</span><span class="o">);</span>
  <span class="o">}</span>
<span class="o">});</span>
</pre></div>
</div>
</div>
<div class="section" id="messages-as-blobs">
<h2>Messages as BLOBs<a class="headerlink" href="#messages-as-blobs" title="Permalink to this headline">¶</a></h2>
<p>If you need to deserialize a ROS message BLOB, it is important to remember that
Java is a big endian virtual machine. When supplying the <tt class="docutils literal"><span class="pre">ByteBuffer</span></tt> to the
<a class="reference external" href="javadoc/org/ros/message/MessageDeserializer.html#">MessageDeserializer</a>, make sure that order is set to
little endian.</p>
<div class="highlight-java"><div class="highlight"><pre><span class="n">Node</span> <span class="n">node</span><span class="o">;</span>
<span class="kt">byte</span><span class="o">[]</span> <span class="n">messageData</span><span class="o">;</span>

<span class="o">...</span>

<span class="n">ByteBuffer</span> <span class="n">buffer</span> <span class="o">=</span> <span class="n">ByteBuffer</span><span class="o">.</span><span class="na">wrap</span><span class="o">(</span><span class="n">messageData</span><span class="o">);</span>
<span class="n">buffer</span><span class="o">.</span><span class="na">order</span><span class="o">(</span><span class="n">ByteOrder</span><span class="o">.</span><span class="na">LITTLE_ENDIAN</span><span class="o">);</span>
<span class="n">PointCloud2</span> <span class="n">msg</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="na">getMessageSerializationFactory</span><span class="o">()</span>
    <span class="o">.</span><span class="na">newMessageDeserializer</span><span class="o">(</span><span class="n">sensor_msgs</span><span class="o">.</span><span class="na">PointCloud</span><span class="o">.</span><span class="na">_TYPE</span><span class="o">)</span>
        <span class="o">.</span><span class="na">deserialize</span><span class="o">(</span><span class="n">buffer</span><span class="o">);</span>
</pre></div>
</div>
</div>
</div>


      </div>
      <div class="bottomnav">
      
        <p>
        «&#160;&#160;<a href="getting_started.html">Getting started</a>
        &#160;&#160;::&#160;&#160;
        <a class="uplink" href="index.html">Contents</a>
        </p>

      </div>

    <div class="footer">
        &copy; Copyright 2012, Google, Inc..
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>